const crypto = require('crypto')
let vertoken = require("../utils/token")
const { Query } = require('../utils/query')
const { RespError, RespSuccess, RespData } = require('../utils/resp')
const { AuthErrStatus, CommonErrStatus } = require('../utils/error')
const moment = require("moment")
const fs = require('fs')
const path = require('path')

class library {
  // 增加
  async libraryAdd (ctx, next) {
    let { nickName } = ctx.body
    let { body } = ctx.request
    try {
      // 构建新增SQL语句
      // 插入产品库表(product_library)中
      const sql_set_library = `INSERT INTO product_library SET ?`
      // 入参
      let libraryParams = {
        ...body,
        createBy: nickName,
        createTime: moment().format("YYYY-MM-DD HH:mm:ss"),
        updateTime: moment().format("YYYY-MM-DD HH:mm:ss"),
        updateBy: nickName,
      }
      // 执行新增
      const result = await Query(sql_set_library, libraryParams)
      console.log('result', result)
      let data = {
        rows: result,
        total: 0
      }
      return RespData(ctx, data)
    } catch {
      return RespError(ctx, CommonErrStatus.SERVER_ERR)
    }
  }
  // 删除
  async libraryDelete (ctx, next) {
    let { ids } = ctx.params
    console.log('ids', ids)
    try {
      // 批量删除产品
      let str = `'${ids.replace(/,/g, "','")}'`
      // 构建删除SQL语句
      let delSql = `DELETE FROM product_library WHERE library_id IN(${ids});`
      // 执行查询
      let result = await Query(delSql, [])
      console.log('result', result)
      let data = {
        massage: '删除成功',
        total: 0
      }
      return RespData(ctx, data)
    } catch {
      return RespError(ctx, CommonErrStatus.SERVER_ERR)
    }
  }
  // 修改
  async libraryEdit (ctx, next) {
    let { body } = ctx.request
    let { nickName } = ctx.body
    try {
      // 构建新增SQL语句
      const sql_set_library = `UPDATE jwzs_database.product_library SET ? WHERE library_id = ${body.libraryId}`
      // 入参
      delete body.libraryId
      let libraryParams = {
        ...body,
        updateTime: moment().format("YYYY-MM-DD HH:mm:ss"),
        updateBy: nickName,
      }
      // 执行新增
      const result = await Query(sql_set_library, libraryParams)
      console.log('result', result)
      const data = {
        rows: result,
        total: 0
      }
      return RespData(ctx, data)
    } catch {
      return RespError(ctx, CommonErrStatus.SERVER_ERR)
    }
  }
  // 查询 - 产品列表
  async libraryList (ctx, next) {
    let { pageNum, pageSize, status, title, classifyId } = ctx.request.query
    const offset = (pageNum - 1) * pageSize // 偏移量
    const wherTitle = `WHERE product_library.title LIKE '%${title}%'`
    const statusQuery = status ? `AND product_library.status = ${status}` : ''
    const classifyIdQuery = classifyId ? `AND product_library.classify_id = ${classifyId}` : ''
    try {
      // 构建查询SQL语句
      const countSql = `SELECT COUNT(*) AS total FROM product_library ${wherTitle} ${statusQuery};`
      const listSql = `SELECT 
        product_library.*, 
        product_classify.title AS classify_name
      FROM 
        product_library
        INNER JOIN
        product_classify
      ON 
		    product_library.classify_id = product_classify.classify_id
        ${wherTitle} ${statusQuery} ${classifyIdQuery}
      ORDER BY product_library.create_time DESC LIMIT ${offset}, ${pageSize};`
      // 执行查询
      let totalResult = (await Query(countSql))[0].total
      let listResult = await Query(listSql, [offset, pageSize])
      let data = {
        rows: listResult,
        total: totalResult
      }
      return RespData(ctx, data)
    } catch {
      return RespError(ctx, CommonErrStatus.SERVER_ERR)
    }
  }
}

module.exports = new library()